package com.school.util;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ContextListener implements ServletContextListener{    


	public ContextListener() {
	}    
	    
	private java.util.Timer timer = null;      
     
	/**  f
	 * 初始化定时器  
	 * 
	 */    
	@Override    
	public void contextInitialized(ServletContextEvent arg0) {    
		        
	     /**   
	     * 定时器   
	     */      
	    timer = new java.util.Timer(true);      
	          
	    arg0.getServletContext().log("定时器已启动");     
	       
	    //设置执行时间    
	    Calendar calendar =Calendar.getInstance();    
	    int year = calendar.get(Calendar.YEAR);    
	    int month = calendar.get(Calendar.MONTH);    
	    int day =calendar.get(Calendar.DAY_OF_MONTH);//每天    
	    calendar.set(year, month, day, 06, 00, 00);    
	    Date date = calendar.getTime();  
	    int period = 600000;    
	    // int period = 6000;
	    timer.schedule(new DelFileTask(arg0.getServletContext()), date, period);  
	    arg0.getServletContext().log("已经添加任务调度表");      
	        
	}    

	/**  
	 * 销毁  
	 */    
	@Override    
	public void contextDestroyed(ServletContextEvent arg0){    
	
	    timer.cancel();    
	    arg0.getServletContext().log("定时器销毁");    
	}    
}  